/*
 * @Descripttion: 
 * @Mender: zhtao
 * @Date: 2020-05-21 18:45:53
 * @LastEditors: zhtao
 * @LastEditTime: 2020-06-04 10:58:08
 */
import * as API_Address from '../../api/address'
import * as API_Members from '../../api/members'
import * as API_Trade from '../../api/trade'
import {
  Foundation,
  RegExp
} from '../../ui-utils/index'
let util = require('../../utils/util.js')
const App = getApp()

Page({
  data: {
    finished: false,
    from: '', // 页面涞源
    honourList: [],
    searchType:'时间',      // 赛选类型，
    showSearchType: false, // 是否显示搜索类型 
    sort:1,//排序方式,
    state:'',//状态
    lat: 12, //  纬度
    lon: 12 ,// 经度
    actionNum: 0, // action class
    state:'',//状态
    typeId:0, 
  },
  //确定删除
  confirmDel: util.throttle(function () {
    let that = this
    API_Address.deleteAddress(that.data.addr_id).then(() => {
      that.setData({
        isdelAddress: true
      })
      wx.showToast({
        title: '删除成功'
      })
      that.getAddressList()
    })
  }),
  //搜索类型
  // 暂时无此功能
  // isShowSearchType(){this.setData({showSearchType:!this.data.showSearchType,sort:1})},
  // searchTypetime(){this.setData({searchType:'时间',showSearchType:false,sort:1})},
  // searchTypefenrun(){this.setData({searchType:'分润比例',showSearchType:false,sort:2})},
  // searchTypeDistance(){this.setData({searchType:'距离',showSearchType:false,sort:3})},
  
  
  searchTypefenrun(){
	API_Members.getVipShop({
	  lat:this.data.lat,
	  lon:this.data.lon,
	  sort:1
	}).then((response) => {
	  this.setData({
	    honourList: response,
	  })
	  this.setData({
		  actionNum: 2
	  })
	})
  },
  searchTypetime(){
  	API_Members.getVipShop({
  	  lat:this.data.lat,
  	  lon:this.data.lon,
  	  sort:2
  	}).then((response) => {
  	  this.setData({
  	    honourList: response,
  	  })
	  
	  this.setData({
	  	actionNum: 1
	  })
  	})
  },
  
  searchTypeDistance(){
  	API_Members.getVipShop({
  	  lat:this.data.lat,
  	  lon:this.data.lon,
  	  sort:3
  	}).then((response) => {
  	  this.setData({
  	    honourList: response,
  	  })
	  this.setData({
		  actionNum: 3
	  })
  	})
  },
  switchTab(event) {
    console.log(event);
    
    let index = parseInt(event.currentTarget.dataset.index);
    console.log(index);
    
    if (index === 0 && index !== this.data.typeId){
      this.setData({
        typeId: index,
        honourList:[]
      })
      this.getVipList()
    }
    if (index === 1 && index !== this.data.typeId) {
      this.setData({
        typeId: index,
        honourList:[]
      })
      this.getVipList()
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) { // 设置页面来源
    this.setData({
      from: options.from || ''
    })
	let that = this
	wx.getLocation({
	        type:'wgs84',// 默认wgs84
		success:function(res){
			that.setData({
				lat: res.latitude,
				lon: res.longitude
			})
		},
		fail:function(res){
			that.setData({
				lat: 12,
				lon: 12
			})
		},
		complete:function(){}
	});
  },
  onShow() { // 获取地址列表
    this.getVipList()
  },
  // vip详情
  goHonourDetail(e) {
    let state = e.currentTarget.dataset.item.vip_status_enum.status;
    if(state==1){
      wx.navigateTo({
        url: "/ucenter/honourDetail/honourDetail?vip_id="+e.currentTarget.dataset.item.member_vip_do.vip_grade_id,
      })
      return;
    }
    
  },
  //重新申请
  reApply(e){
    let shop_id = e.currentTarget.dataset.item.member_vip_do.shop_id;
    wx.navigateTo({
      url: '/pages/shop/apply_member/apply_member?shop_id='+shop_id
    })
  },
  getVipList() {
    if(this.data.typeId===0){
      API_Members.getVipShop({
        lat:this.data.lat,
        lon:this.data.lon,
        sort:this.data.sort
      }).then((response) => {
        let data=[]
        response.forEach(item=>{
          if(item.vip_status_enum.status==1){
            data.push(item);
          }
        })
        this.setData({
          honourList: data,
        })
        console.log(this.data.honourList);
      })
    } 
    if(this.data.typeId===1){
      API_Members.getVipShop({
        lat:this.data.lat,
        lon:this.data.lon,
        sort:this.data.sort
      }).then((response) => {
        let data=[]
        response.forEach(item=>{
          if(item.vip_status_enum.status!=1){
            data.push(item);
          }
        })
        this.setData({
          honourList: data,
        })
        console.log('this.data.honourList');
        
        console.log(this.data.honourList);
      })
    } 
  }
})